package com.spboot.controller;


import com.spboot.entity.Address;
import com.spboot.entity.User;
import com.spboot.service.AddressService;
import com.spboot.service.ShoppingcartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author guocb
 * @since 2020-04-27
 */
@Controller
@RequestMapping("/address")
public class AddressController {

    @Autowired
    AddressService addressService;

    @Autowired
    ShoppingcartService shoppingcartService;

    /**
     * 用户地址
     * @param userid
     * @param model
     * @return
     */
    @RequestMapping("address")
    public String address(Integer userid, Model model,HttpServletRequest request){
        User user = (User) request.getSession().getAttribute("login");
        if(user!=null){
            //右上购物车查询
            model.addAttribute("shoplist",shoppingcartService.ssum(user.Userid));
            model.addAttribute("select",shoppingcartService.selectall(user.Userid));
            model.addAttribute("shu",shoppingcartService.ssum(user.Userid));
            model.addAttribute("zong",shoppingcartService.zong(user.Userid));
        }
        model.addAttribute("addressselect",addressService.saddress(userid));
        return "address";
    }

    /**
     * 显示地址
     * @param addressid
     * @return
     */
    @RequestMapping("updateajax")
    @ResponseBody
    public Address updateajax(Long addressid){
        Address address = new Address();
        address = addressService.getById(addressid);
        return address;
    }

    /**
     * 修改用户地址
     * @param contacts
     * @param phone
     * @param address
     * @param addressid
     * @param userid
     * @return
     */
    @RequestMapping("updateaddress")
    public String update(String contacts, String phone, String address, Integer addressid,Integer userid){
        int shifou = addressService.updateaddress(contacts, phone, address, addressid);
        if(shifou!=0){
            return "address?userid="+userid;
        }
        return "";
    }

    /**
     * 添加用户地址
     * @param address
     * @param userid
     * @param contacts
     * @param phone
     * @return
     */
    @RequestMapping("insertaddress")
    public String insert(String address,Integer userid,String contacts,String phone){
        int shifou = addressService.insertaddress(address, userid, contacts, phone);
        if(shifou!=0){
            return "address?userid="+userid;
        }
        return "";
    }

    /**
     * 删除用户地址
     * @param addressid
     * @return
     */
    @RequestMapping("deleteaddress")
    public String delete(Integer addressid,Integer userid){
        int shifou = addressService.deleteaddress(addressid);
        if(shifou!=0){
            return "address?userid="+userid;
        }
        return "";
    }
}

